package io.wdb.modules.manager.gen.manager;

import java.text.SimpleDateFormat;
import java.util.Date;

public class WsStrUtils {
	
	public static String getServerToken(String serverName) {
		return md5(getCurrentDateStr() + "-" + serverName);
	}

	
	public static String generateToken(String seconds) {
		System.out.println(seconds);
		String encodeStr = "ghello" + seconds;
		return md5(encodeStr);
	}
	
	public static String md5(byte[] source) {
		char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };

		try {
			java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
			md.update(source);
			byte tmp[] = md.digest();
			char str[] = new char[16 * 2];
			int k = 0;

			for (int i = 0; i < 16; i++) {
				byte byte0 = tmp[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}

			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}

	public static String md5(String data) {
		try {
			return md5(data.getBytes("UTF-8"));
		} catch (Exception e) {
			return null;
		}
	}

	public static String getCurrentDateStr() {
		Date currentTime = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
		String dateString = formatter.format(currentTime);
		return dateString;
	}
	
	public static void main(String[] args) {
//		String seconds = System.currentTimeMillis() / 1000 + "";
//		System.out.println(generateToken(seconds));.
		String str = "hello,world";
		System.out.println(md5(str));
	}
}
